package com.clothe.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.clothe.dao.IKindDAO;
import com.clothe.dao.impl.KindDAOImpl;
import com.clothe.database.DataBase;
import com.clothe.dto.Kind;

public class KindService {
	IKindDAO kindDAOImpl = new KindDAOImpl();

	public boolean save(Kind kind) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			kindDAOImpl.setConn(connection);
			flag = kindDAOImpl.save(kind);
			DataBase.commit();
		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;

	}

	public boolean delete(int id) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			kindDAOImpl.setConn(connection);
			flag = kindDAOImpl.delete(id);
			DataBase.commit();
		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;

	}

	public boolean update(Kind kind) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			kindDAOImpl.setConn(connection);
			flag = kindDAOImpl.update(kind);
			DataBase.commit();
		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;

	}

	public Kind get(int id) throws Exception {
		Connection connection = null;
		Kind kind = null;
		try {
			kind = new Kind();
			connection = DataBase.getConn();
			kindDAOImpl.setConn(connection);
			kind = kindDAOImpl.get(id);
			DataBase.commit();
		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return kind;
	}
	
	
	public List<Kind> list() throws Exception {
		Connection connection = null;
		List<Kind> kindlist = null;
		try {
			kindlist = new ArrayList<Kind>();
			connection = DataBase.getConn();
			kindDAOImpl.setConn(connection);
			kindlist = kindDAOImpl.list();
			DataBase.commit();
		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return kindlist;
	}

}
